<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Message extends ORM {


     /*
     * Message belongs to inbox and has one outbox, 
     * 
     */
    protected $_has_many = array(
                'inbox' => array('model' => 'inbox', 'foreign_key' => 'message_id')
    );
    
    protected $_has_one = array(
                'outbox' => array('model' => 'outbox', 'foreign_key' => 'message_id')
    );
    
   
	/**
	 * Rules for the message model. 
	 *
	 * @return array Rules
	 */
        
        //TODO: maybe add some more rules for recipient and sender fields 
	public function rules()
	{
		return array(
			'subject' => array(
				array('not_empty'),
				array('min_length', array(':value', 4)),
				array('max_length', array(':value', 127)),
			),
			'content_message' => array(
				array('not_empty'),
			),
                 
		);
	}

	/**
	 * Filters to run when data is set in this model. In this case title and 
	 * content will use xss cleaner.
         * 
	 * @return array Filters
	 */
        
	public function filters()
	{
		return array(
			'subject' => array(
                                //xss clean
				array(array('Security', 'xss_clean')),
                                
			),
                        'content_message' => array(
                                array(array('Security', 'xss_clean'))
                        ),
		);
	}

	/**
	 * Labels for fields in this model
	 *
	 * @return array Labels
	 */
	public function labels()
	{
		return array(
			'subject'      => __('Subject'),
			'content_message'      => __('Content'),
			'recipient_id' => __('Recipient'),
		);
	}
        
        public function get_bin($user_id, $limit = null, $page = null, $word = null)
        {
            $temp_messages = $this->select('user_profiles.name', 
                                           'user_profiles.surname', 
                                           'user_profiles.description', 
                                           'user_profiles.url',
                                           'user_profiles.user_id',
                                           array(DB::expr('COUNT(inboxes.message_id)'),'user_amout'), 
                                           array('user_profiles.file_id','photo'),
                                           array('files.id','file_id'),
                                           array('files.name','file_name'),
                                           array('files.extension','file_extension'),
                                           array(DB::expr('CASE WHEN `outboxes`.`user_id` = '.$user_id.' THEN `inboxes`.`id` else `outboxes`.`id` END'),'entre_id'),
                                           array(DB::expr('CASE WHEN `outboxes`.`user_id` = '.$user_id.' THEN 0 else 1 END'),'direction')) 
                                  ->join('outboxes')->on('messages.id','=','outboxes.message_id')
                                  ->join('inboxes')->on('messages.id','=','inboxes.message_id')
                                  ->join('user_profiles')->on(DB::expr('CASE WHEN `outboxes`.`user_id` = '.$user_id.' THEN `inboxes`.`user_id` else `outboxes`.`user_id` END'), '=', 'user_profiles.user_id')
                                  ->join('files', 'LEFT')->on('user_profiles.file_id', '=', 'files.id')
                                  ->where_open()
                                    ->where('outboxes.user_id', '=', $user_id)
                                    ->where('outboxes.folder', '=', 1)
                                    ->where('outboxes.deleted', '=', 0)
                                  ->where_close()
                                  ->or_where_open()
                                    ->where('inboxes.user_id', '=', $user_id)
                                    ->where('inboxes.folder', '=', 1)
                                    ->where('inboxes.deleted', '=', 0)
                                    ->or_where_close()
                                 ->group_by('inboxes.message_id')
                                 ->order_by('id','DESC');
                                 
            if ($limit)
            {
                $temp_messages->limit($limit);
                if ($page)
                {
                    $offset = $limit * ($page - 1);
                    $temp_messages->offset($offset);
                }
            }
        
            if ($word)
            {
                $temp_messages->where_open()
                                  ->where('messages.subject', 'LIKE', '%'.$word.'%')
                                  ->or_where('messages.content_message', 'LIKE', '%'.$word.'%')
                              ->where_close();
            }
        
            $temp_messages = $temp_messages->find_all();
            
            $messages = array();
            foreach($temp_messages as $message)
            { 
                $message->photo = Media::get_file_path($message->file_name, 
                                                       $message->file_id, 
                                                       $message->file_extension, 
                                                        true, 
                                                        'mini_square', 
                                                        false);
                $messages[] = $message;
            }
            return $messages;
        }
}